package cn.edu.ren.controller;

import cn.edu.ren.pojo.Person;
import cn.edu.ren.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: cn.edu.ren.controller
 * @ClassName: PersonController
 * @Description:
 * @author: RZS
 * @date: 2021/5/19  21:55
 */
@RestController
@RequestMapping("/user")
public class PersonController {

    @Autowired
    private IPersonService personService;

    @GetMapping("select_all")
    public List<Person> findAll(){
        return personService.findAll();
    }

    @GetMapping("/select_id/{id}")
    public Person findById(@PathVariable Integer id){
        return personService.findById(id);
    }

    @PostMapping("/add")
    public List<Person> addPerson(Person person){
        System.err.println(person.toString());
        personService.add(person);
        return findAll();
    }

    @PostMapping("/update")
    public Person changeByUsername(String username, Integer id){
        personService.changeUsernameById(username, id);
        return findById(id);
    }

    @GetMapping("/delete/{id}")
    public List<Person> remove(@PathVariable Integer id){
        personService.removeById(id);
        return findAll();
    }

}
